<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\common\manager;

use app\common\model\MoneyLog;
use app\common\model\StoreAccount;
use app\common\model\StoreMoneyLog;
use app\common\model\User;
use think\Db;
use think\Exception;

class StoreMoneyManager extends BaseManager
{
    /**
     * 积分变更类型 1=增加,2=减少
     */
    CONST TYPE_INCREASE = 2;
    CONST TYPE_DECREASE = 1;

    /**
     * 金额变动类型 :
     * 1 = 奖励
     * 2 = 冻结
     * 3 = 提现
     * 4 = 组队 [41=收入,42=支出]
     * 5 = 比赛
     * 6 = 购买体验卡
     * 7 = 解冻返回
     */
    CONST TYPE_REWARD = 1;
    CONST TYPE_FREEZE = 2;
    CONST TYPE_WITHDRAWAL = 3;
    CONST TYPE_UNFREEZE = 4;
    CONST TYPE_TEAM = 5;
    CONST TYPE_MATCH = 6;
    CONST TYPE_ASSISTANT_REWARD = 7;
    CONST TYPE_ASSISTANT = 8;
    CONST TYPE_REWARD_MESSAGE = '门店%s 于 %s 获取奖励 %.2f';
    CONST TYPE_FREEZE_MESSAGE = '门店%s 于 %s 冻结 %.2f';
    CONST TYPE_WITHDRAWAL_MESSAGE = '门店%s 于 %s 提现 %.2f';
    CONST TYPE_UNFREEZE_MESSAGE = '门店%s 于 %s 解冻 %.2f';
    CONST TYPE_TEAM_MESSAGE = '门店%s 于 %s 组队成功,收入%.2f';
    CONST TYPE_MATCH_MESSAGE = '门店%s 于 %s 举办赛事成功,收入 %.2f';
    CONST TYPE_ASSISTANT_MESSAGE = '门店%s 于 %s 获取助教分成,收入 %.2f';
    CONST TYPE_ASSISTANT_REWARD_MESSAGE = '门店%s 于 %s 获取助教上级分成,收入 %.2f';
    CONST MEMO_COLLECTION = [

        self::TYPE_REWARD => [
            'value'=>self::TYPE_REWARD,
            'message'=>self::TYPE_REWARD_MESSAGE,
        ],

        self::TYPE_FREEZE => [
            'value'=>self::TYPE_FREEZE,
            'message'=>self::TYPE_FREEZE_MESSAGE,
        ],

        self::TYPE_WITHDRAWAL => [
            'value'=>self::TYPE_WITHDRAWAL,
            'message'=>self::TYPE_WITHDRAWAL_MESSAGE,
        ],

        self::TYPE_UNFREEZE => [
            'value'=>self::TYPE_UNFREEZE,
            'message'=>self::TYPE_UNFREEZE_MESSAGE,
        ],

        self::TYPE_TEAM => [
            'value'=>self::TYPE_TEAM,
            'message'=>self::TYPE_TEAM_MESSAGE,
        ],

        self::TYPE_MATCH => [
            'value'=>self::TYPE_MATCH,
            'message'=>self::TYPE_MATCH_MESSAGE,
        ],

    ];

    /**
     * @param StoreAccount $storeAccount 商户账户
     * @param int $storeId 用户id
     * @param float $money 金额
     * @param int $outIn 1=收入,2=支出
     * @param int $detail 1=奖励  2=冻结  3=提现 4=解冻 5=组队  6=比赛
     * @param string $message 消息
     * @param array $extend 扩展字段
     * @param boolean $trans 是否开启事务
     * @return void
     */
    public static function StoreMoneyLog(StoreAccount $storeAccount,int $storeId, float $money,int $outIn,int $detail,string $message,array $extend=[],bool $trans=true):array
    {
        if ($trans){
            Db::startTrans();
        }
        try {
            StoreMoneyLog::create([
                'store_id' => $storeId,
                'type' => $outIn,
                'detail' => $detail,
                'money' => $money,
                'before' => $storeAccount->total,
                'after' => bcadd($storeAccount->total,$money,2),
                'memo' => $message,
                'relation_id' => isset($extend['relation_id']) ?? 0,
                'relation_order' => isset($extend['relation_order']) ?? '',
                'createtime' => time(),
            ]);

            if ($trans){
                Db::commit();
            }
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
        }catch (Exception $exception){
            if ($trans){
                Db::rollback();
            }
            self::setStatusCode(self::$failCode);
            self::setMessage($exception->getMessage());
        }

        return ['code'=>self::getStatusCode(),'message'=>self::getMessage()];
    }
}